<template>
  <div>
    <el-dialog title="" :visible.sync="dialogVisible" width="400px" :before-close="handleClose" append-to-body>
      <div>
        <div id="Qrcode" ref="Qrcode" class="qr_code" />
      </div>
    </el-dialog>
  </div>
</template>

<script>
// import QRCode from 'qrcodejs2';
import { getCookieTenant } from '@/lib/auth';
export default {
  props: {
    tenantid: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      dialogVisible: false,
      wxUrl: 'https://open.weixin.qq.com/connect/qrconnect?',
      wxConfig: {
        appid: WEB_CONFIG.VUE_APP_WX_ID,
        redirect_uri:
          WEB_CONFIG.VUE_APP_WX_REDIRECT_URI + '?tenantId=' + this.tenantId,
        response_type: 'code',
        scope: 'snsapi_login',
        state: new Date().toString()
      },
      direvtor: ''
    };
  },
  mounted() {},
  methods: {
    handleClose() {
      this.dialogVisible = false;
      this.$refs.Qrcode.innerHTML = '';
    },
    getLoginUrl() {
      // const url = `${WEB_CONFIG.VUE_APP_ODIC_HOST}${
      if (!window.location.origin) {
        window.location.origin =
          window.location.protocol +
          '//' +
          window.location.hostname +
          (window.location.port ? ':' + window.location.port : '');
      }
      const url = `${window.location.origin}/login.html#/wx/register`;
      return url;
    },
    gowxlogin() {
      console.log(getCookieTenant());
      this.wxConfig.redirect_uri = `${
        WEB_CONFIG.VUE_APP_WX_REDIRECT_URI
      }/api/account/wechat-login?tenantId=${getCookieTenant()}&appId=${
        this.wxConfig.appid
      }&returnUrl=${encodeURIComponent(
        this.$route.query.ReturnUrl
      )}&registerUrl=${encodeURIComponent(this.getLoginUrl())}&rememberMe=true`;
      this.jumptologin();
      // this.showDialog();
    },
    // 弹窗
    showDialog() {
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.createdQrCode();
      });
    },
    // 跳转登陆
    jumptologin() {
      const url = `${this.wxUrl}appid=${
        this.wxConfig.appid
      }&redirect_uri=${encodeURIComponent(
        this.wxConfig.redirect_uri
      )}&response_type=${this.wxConfig.response_type}&scope=${
        this.wxConfig.scope
      }`;
      window.location.href = url;
    },
    // 生成二维码
    createdQrCode() {
      new WxLogin({
        self_redirect: true,
        id: 'Qrcode',
        appid: this.wxConfig.appid,
        scope: this.wxConfig.scope,
        redirect_uri: this.wxConfig.redirect_uri,
        state: this.wxConfig.state,
        style: '',
        href: ''
      });
    }
  }
};
</script>

<style scoped>
.login_bottom_box {
  text-align: center;
}
.login_icon {
  font-size: 30px;
  color: #1aad19;
  cursor: pointer;
}
.qr_code {
  text-align: center;
}
.qr_code img {
  display: block;
}
</style>
